{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "207feecc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
      " 0 0 0 0 0 1 1 1 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0\n",
      " 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1]\n",
      "0.7977288857345636\n",
      "[[0.82041491 0.17958509]\n",
      " [0.84029613 0.15970387]\n",
      " [0.79819342 0.20180658]\n",
      " [0.62989192 0.37010808]\n",
      " [0.61636611 0.38363389]]\n",
      "[0.17958509 0.15970387 0.20180658 ... 0.04220544 0.09782449 0.63586739]\n",
      "[[ 2.41952469e-05  8.16881491e-03  1.04320950e-02 -2.54894468e-03\n",
      "  -1.10120609e-04]]\n",
      "[-1.43393291e-06]\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_excel(\"D:\\贺之恒的python\\pppython\\python商业数据分析\\@Python大数据分析与机器学习商业案例实战\\第4章 逻辑回归模型\\源代码汇总_PyCharm格式\\股票客户流失.xlsx\")\n",
    "df.head()\n",
    "\n",
    "# 2.划分特征变量和目标变量\n",
    "X = df.drop(columns='是否流失') \n",
    "y = df['是否流失']   \n",
    "\n",
    "# 3.划分训练集和测试集\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)\n",
    "\n",
    "X_train.head()\n",
    "y_train.head()\n",
    "X_test.head()\n",
    "y_test.head()\n",
    "\n",
    "# 4.模型搭建\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "model = LogisticRegression()\n",
    "model.fit(X_train, y_train)\n",
    "\n",
    "# 5.模型使用1 - 预测数据结果\n",
    "y_pred = model.predict(X_test)\n",
    "print(y_pred[0:100])\n",
    "\n",
    "a = pd.DataFrame()\n",
    "a['预测值'] = list(y_pred)\n",
    "a['实际值'] = list(y_test)\n",
    "a.head()\n",
    "\n",
    "from sklearn.metrics import accuracy_score\n",
    "score = accuracy_score(y_pred, y_test)\n",
    "print(score)\n",
    "\n",
    "model.score(X_test, y_test)\n",
    "\n",
    "# 6.模型使用2 - 预测概率\n",
    "y_pred_proba = model.predict_proba(X_test)  \n",
    "print(y_pred_proba[0:5])\n",
    "\n",
    "a = pd.DataFrame(y_pred_proba, columns=['不流失概率', '流失概率'])\n",
    "a.head()\n",
    "\n",
    "print(y_pred_proba[:,1])\n",
    "\n",
    "# 7.查看各个特征变量的系数（额外知识点，供参考）\n",
    "print(model.coef_)\n",
    "print(model.intercept_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "eb9bca93",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "========================================\n",
      "逻辑回归模型\n",
      "========================================\n",
      "准确率: 0.7949\n",
      "\n",
      "分类报告:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.82      0.92      0.87      1036\n",
      "           1       0.66      0.46      0.54       373\n",
      "\n",
      "    accuracy                           0.79      1409\n",
      "   macro avg       0.74      0.69      0.70      1409\n",
      "weighted avg       0.78      0.79      0.78      1409\n",
      "\n",
      "\n",
      "前5个预测结果:\n",
      "   实际值  预测值     不流失概率      流失概率\n",
      "0    1    0  0.718601  0.281399\n",
      "1    0    0  0.954246  0.045754\n",
      "2    0    0  0.968075  0.031925\n",
      "3    1    1  0.210856  0.789144\n",
      "4    0    0  0.956798  0.043202\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import accuracy_score, classification_report\n",
    "\n",
    "# 1. 数据加载\n",
    "df = pd.read_excel(\"D:\\贺之恒的python\\pppython\\python商业数据分析\\@Python大数据分析与机器学习商业案例实战\\第4章 逻辑回归模型\\源代码汇总_PyCharm格式\\股票客户流失.xlsx\")\n",
    "\n",
    "# 2. 数据预处理\n",
    "X = df.drop(columns='是否流失') \n",
    "y = df['是否流失']\n",
    "\n",
    "scaler = StandardScaler()\n",
    "X_scaled = scaler.fit_transform(X)\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)\n",
    "\n",
    "# 3. 逻辑回归模型\n",
    "print(\"=\" * 40)\n",
    "print(\"逻辑回归模型\")\n",
    "print(\"=\" * 40)\n",
    "\n",
    "lr_model = LogisticRegression(random_state=42, max_iter=1000)\n",
    "lr_model.fit(X_train, y_train)\n",
    "\n",
    "# 预测和评估\n",
    "y_pred = lr_model.predict(X_test)\n",
    "y_pred_proba = lr_model.predict_proba(X_test)\n",
    "\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(f\"准确率: {accuracy:.4f}\")\n",
    "print(\"\\n分类报告:\")\n",
    "print(classification_report(y_test, y_pred))\n",
    "\n",
    "# 显示前5个预测结果\n",
    "results = pd.DataFrame({\n",
    "    '实际值': y_test.values,\n",
    "    '预测值': y_pred,\n",
    "    '不流失概率': y_pred_proba[:, 0],\n",
    "    '流失概率': y_pred_proba[:, 1]\n",
    "})\n",
    "print(\"\\n前5个预测结果:\")\n",
    "print(results.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "92dae418",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "========================================\n",
      "随机森林模型\n",
      "========================================\n",
      "准确率: 0.7686\n",
      "\n",
      "分类报告:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.82      0.88      0.85      1036\n",
      "           1       0.58      0.47      0.52       373\n",
      "\n",
      "    accuracy                           0.77      1409\n",
      "   macro avg       0.70      0.67      0.68      1409\n",
      "weighted avg       0.76      0.77      0.76      1409\n",
      "\n",
      "\n",
      "特征重要性:\n",
      "              特征       重要性\n",
      "2      上月交易佣金（元）  0.324172\n",
      "0        账户资金（元）  0.233147\n",
      "3      累计交易佣金（元）  0.228357\n",
      "1  最后一次交易距今时间（天）  0.131564\n",
      "4     本券商使用时长（年）  0.082760\n",
      "\n",
      "前5个预测结果:\n",
      "   实际值  预测值  不流失概率  流失概率\n",
      "0    1    1   0.26  0.74\n",
      "1    0    0   1.00  0.00\n",
      "2    0    0   0.97  0.03\n",
      "3    1    1   0.01  0.99\n",
      "4    0    0   0.90  0.10\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import accuracy_score, classification_report\n",
    "\n",
    "# 1. 数据加载\n",
    "df = pd.read_excel(\"D:\\贺之恒的python\\pppython\\python商业数据分析\\@Python大数据分析与机器学习商业案例实战\\第4章 逻辑回归模型\\源代码汇总_PyCharm格式\\股票客户流失.xlsx\")\n",
    "\n",
    "# 2. 数据预处理\n",
    "X = df.drop(columns='是否流失') \n",
    "y = df['是否流失']\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "\n",
    "# 3. 随机森林模型\n",
    "print(\"=\" * 40)\n",
    "print(\"随机森林模型\")\n",
    "print(\"=\" * 40)\n",
    "\n",
    "rf_model = RandomForestClassifier(n_estimators=100, random_state=42)\n",
    "rf_model.fit(X_train, y_train)\n",
    "\n",
    "# 预测和评估\n",
    "y_pred = rf_model.predict(X_test)\n",
    "y_pred_proba = rf_model.predict_proba(X_test)\n",
    "\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(f\"准确率: {accuracy:.4f}\")\n",
    "print(\"\\n分类报告:\")\n",
    "print(classification_report(y_test, y_pred))\n",
    "\n",
    "# 特征重要性\n",
    "feature_importance = pd.DataFrame({\n",
    "    '特征': df.drop(columns='是否流失').columns,\n",
    "    '重要性': rf_model.feature_importances_\n",
    "}).sort_values('重要性', ascending=False)\n",
    "\n",
    "print(\"\\n特征重要性:\")\n",
    "print(feature_importance)\n",
    "\n",
    "# 显示前5个预测结果\n",
    "results = pd.DataFrame({\n",
    "    '实际值': y_test.values,\n",
    "    '预测值': y_pred,\n",
    "    '不流失概率': y_pred_proba[:, 0],\n",
    "    '流失概率': y_pred_proba[:, 1]\n",
    "})\n",
    "print(\"\\n前5个预测结果:\")\n",
    "print(results.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "da954273",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "========================================\n",
      "支持向量机模型\n",
      "========================================\n",
      "准确率: 0.7928\n",
      "\n",
      "分类报告:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.81      0.94      0.87      1036\n",
      "           1       0.69      0.40      0.50       373\n",
      "\n",
      "    accuracy                           0.79      1409\n",
      "   macro avg       0.75      0.67      0.69      1409\n",
      "weighted avg       0.78      0.79      0.77      1409\n",
      "\n",
      "\n",
      "前5个预测结果:\n",
      "   实际值  预测值     不流失概率      流失概率\n",
      "0    1    0  0.776439  0.223561\n",
      "1    0    0  0.831388  0.168612\n",
      "2    0    0  0.839244  0.160756\n",
      "3    1    1  0.187705  0.812295\n",
      "4    0    0  0.826374  0.173626\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.metrics import accuracy_score, classification_report\n",
    "\n",
    "# 1. 数据加载\n",
    "df = pd.read_excel(\"D:\\贺之恒的python\\pppython\\python商业数据分析\\@Python大数据分析与机器学习商业案例实战\\第4章 逻辑回归模型\\源代码汇总_PyCharm格式\\股票客户流失.xlsx\")\n",
    "\n",
    "# 2. 数据预处理\n",
    "X = df.drop(columns='是否流失') \n",
    "y = df['是否流失']\n",
    "\n",
    "scaler = StandardScaler()\n",
    "X_scaled = scaler.fit_transform(X)\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)\n",
    "\n",
    "# 3. 支持向量机模型\n",
    "print(\"=\" * 40)\n",
    "print(\"支持向量机模型\")\n",
    "print(\"=\" * 40)\n",
    "\n",
    "svm_model = SVC(probability=True, random_state=42)\n",
    "svm_model.fit(X_train, y_train)\n",
    "\n",
    "# 预测和评估\n",
    "y_pred = svm_model.predict(X_test)\n",
    "y_pred_proba = svm_model.predict_proba(X_test)\n",
    "\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(f\"准确率: {accuracy:.4f}\")\n",
    "print(\"\\n分类报告:\")\n",
    "print(classification_report(y_test, y_pred))\n",
    "\n",
    "# 显示前5个预测结果\n",
    "results = pd.DataFrame({\n",
    "    '实际值': y_test.values,\n",
    "    '预测值': y_pred,\n",
    "    '不流失概率': y_pred_proba[:, 0],\n",
    "    '流失概率': y_pred_proba[:, 1]\n",
    "})\n",
    "print(\"\\n前5个预测结果:\")\n",
    "print(results.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47cc67e1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
